package com.morpheus.redis.lettuce.cluster;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

public class OnEnableLettuceClusterCondition extends SpringBootCondition {
	private static final Logger LOGGER = LoggerFactory.getLogger(OnEnableLettuceClusterCondition.class);

	@Override
	public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
		Environment environment = context.getEnvironment();
		boolean isCluster = environment.containsProperty("spring.redis.cluster.nodes")
				|| environment.containsProperty("spring.redis.cluster.max-redirects")
				|| environment.containsProperty("spring.redis.cluster.maxRedirects");
		boolean isLettuce = environment.containsProperty("spring.redis.lettuce.pool.max-idle")
				|| environment.containsProperty("spring.redis.lettuce.pool.maxIdle")
				|| environment.containsProperty("spring.redis.lettuce.pool.min-idle")
				|| environment.containsProperty("spring.redis.lettuce.pool.minIdle")
				|| environment.containsProperty("spring.redis.lettuce.pool.max-active")
				|| environment.containsProperty("spring.redis.lettuce.pool.maxActive")
				|| environment.containsProperty("spring.redis.lettuce.pool.max-wait")
				|| environment.containsProperty("spring.redis.lettuce.pool.maxWait");
		ConditionOutcome conditionOutcome = new ConditionOutcome(isCluster && isLettuce, "Enable Lettuce Cluster");
		return conditionOutcome;
	}
}
